package com.musicapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
    private ItemAdapter.OnItemClickListener mOnItemClickListen;
    List<SongModel> songList = FirstFragment.songList;

    public ListAdapter( ) {
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    public void setmOnItemClickListen(ItemAdapter.OnItemClickListener onItemClickListen){
        this.mOnItemClickListen = onItemClickListen;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listlayout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.music.setText(songList.get(position).getMusicname());
        holder.author.setText(songList.get(position).getAuthor());
//        holder.number.setText(songList.size());
        if(mOnItemClickListen!=null){
            holder.delete.setOnClickListener(v->mOnItemClickListen.onItemClick(v,position));
        }
    }

    @Override
    public int getItemCount() {
        return songList.size();
    }

    public void removeItem(int position) {
        songList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, songList.size());
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView music;
        private TextView author;
        private ImageView delete;
//        private TextView number;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            music = itemView.findViewById(R.id.music);
            author = itemView.findViewById(R.id.author);
//            number = itemView.findViewById(R.id.number);
            delete = itemView.findViewById(R.id.delete);
        }
    }
}